﻿var rtdataHost = "http://rtdata.vsdn.tv380.com/";
$(document).ready(function() {
    $("#type").bind("change", function() {
        if ($(this).val() == 0) {
            $("#labelend").hide();
            $('#end').val('');
//            $("#rp1,#rp2,#rp3,#rp4").show();
//            $("#rp6,#rp7,#rp5").hide();
            $("#labeldaytoolbar").show();
            $("#labelmonthtoolbar,#labeltimetoolbar").hide();
            $("#condition1").trigger("click");
        }
        else if ($(this).val() == 1) {
            $("#labelend").hide();
            $('#end').val('');
//            $("#rp1,#rp2,#rp3,#rp4,#rp5,#rp6,#rp7").show();
//            $("#rp5").hide();
            $("#labelmonthtoolbar").show();
            $("#labeldaytoolbar,#labeltimetoolbar").hide();
            $("#condition4").trigger("click");
        }
        else {
            $("#labelend").show();
//            $("#rp1,#rp3").show();
//            $("#rp2,#rp4,#rp7,#rp5,#rp6").hide();
            $("#labeltimetoolbar").show();
            $("#labelmonthtoolbar,#labeldaytoolbar").hide();
            $("#condition6").trigger("click");
        }
    });
    $("#type").trigger("change");
});
function validate(start, end) {
    var type = $('#type').val();
    if (type == "0") {
        if (!checkDate(start, 8)) {
            alert("请填写正确的日期！");
            return false;
        }
    } else if (type == "1") {
        if (!checkDate(start, 6)) {
            alert("请填写正确的日期！");
            return false;
        }
    } else {
        if (!checkDate(start, 8)) {
            alert("请填写正确的日期！");
            return false;
        }
        if (!checkDate(end, 8)) {
            alert("请填写正确的日期！");
            return false;
        }
        if (start > end) {
            alert("请填写正确的开始、结束日期！");
            return false;
        }
    }
    return true;
}
function show(dsname) {
    var type = $('#type').val();
    var start = $('#start').val();
    var end = $('#end').val();
    var tickInterval = "";
    var xformatString = "";
    var barWidth =1;
    if (validate(start, end)) {
         if(type =="0")//日报/实时监测
         {
            end = start;
            tickInterval = "2 hours";
            xformatString = "%H:%M";
            barWidth = 5;
         }
         else if(type == "1")//月报
         {
        	end = start;
            tickInterval = "24 hours";
            xformatString = "%Y/%#m/%#d %H:%M";
            barWidth = 3;
         }
         else//时间段
         {
        	 tickInterval = "6 hours";
             xformatString = "%Y/%#m/%#d %H:%M";
             barWidth = 3;
         }
         //取报表数据         
         var uri = "/rtreport/live/dsname/"+dsname+"/type/"+type+"/start/"+start+"/end/"+end+"/";
         //alert(uri);
         $.get(uri, function(data){
        	 var goog = getDataArray(data);
        	 //alert(goog);
        	 //绘图        	 
        	 $("#chart1").html("");//清除图像
        		$.jqplot.config.enablePlugins = true;
        		 plot = $.jqplot('chart1', [goog], { 
        			 //seriesDefaults:{renderer:$.jqplot.BarRenderer, rendererOptions:{barWidth:barWidth,barPadding:1, barMargin:1}},//for barRenderer
        			 	seriesDefaults: {
				 			 color: '#00BB00',
		        			 lineWidth: 1,
		        			 fill: true,
		        			 fillColor: '#00BB00',
		        			 showMarker: false, 
		        			 shadow: false
		        			 },
        		         title: getTitle(dsname),//+"("+start+"-"+end+")",
        		         axes: {
        		             xaxis: {
        		               renderer:$.jqplot.DateAxisRenderer,
        		               min:goog[0][0],
        		               tickInterval: tickInterval, 
        		               rendererOptions:{tickRenderer:$.jqplot.CanvasAxisTickRenderer},
		                       tickOptions:{formatString:xformatString, fontSize:'10pt', fontFamily:'Tahoma', angle:-40, fontWeight:'normal', fontStretch:1}
        		             },
        		             yaxis: {       
        		            	 //renderer: $.jqplot.LogAxisRenderer,
        		                 min:0,
        		                 max:getDataMax(goog,1),
        		                 tickRenderer: $.jqplot.CanvasAxisTickRenderer,
	  	      		               tickOptions: {
        		                	 formatString:'%d',
	  	      		                   enableFontSupport: true,
	  	      		                     angle: -30
	  	      		                 }
        		             }
        		         },
        		         cursor:{zoom:true,
        		        	 clickReset:true,
        		        	 showTooltip: true, // 是否显示提示信息栏 
        		        	 followMouse: true, //光标的提示信息栏是否随光标（鼠标）一起移动 
        		        	 tooltipLocation: 'ne'
        		        	 }
        		    });
        	 
        	}); 
    }
    else {
        
    }  
    return false;
}
function getDataArray(data)
{
	var arr = data.split(";");
	 for(i=0;i<arr.length;i++)
	 {
		 arr[i] = arr[i].split(",");//new Array(Number(arr[i].split(",")[0]),Number(arr[i].split(",")[1]));
	 }
	 return arr;
}

var col=0;    
function cmp(a,b){     
	return a[col]-b[col];     
}
function getDataMax(data,i)
{
	l = data.length-1;
	col   =   i;     
	data   =   data.sort(cmp);
	var max = Number(data[l][i])+ 1;
	return max;
} 

function getTitle(dsname)
{
	var title = "";
	if(dsname == "online")
		title = "<h4>直播在线人数</h4>";
	else
		title = "<h4>直播在线人数</h4>";
	return title;
}
function getDays(y,m)
{
    return new Date(y,m,0).getDate();
}
function getDate(addDay) {
    var d = new Date();
    d.setTime(d.getTime() + addDay * 1000 * 60 * 60 * 24);
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    if (month < 10) month = "" + 0 + month;
    if (day < 10) day = "" + 0 + day;
    return "" + year + month + day;
}
function getMonth(addMonth) {
    var d = new Date();
    var day = d.getDate();
    if (day == 31)
        d.setDate(day - 2);
    d.setMonth(d.getMonth() + addMonth);
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    if (month < 10) month = "" + 0 + month;
    return "" + year + month;
}
function setDate(start, end) {
    $('#start').val(start);
    $('#end').val(end);
    return false;
}
function checkDate(strDate, strLen) {
    //type=0 8位日期，1 6位月份，2 8位日期

    var check = false;
    if (strLen == 8) {
        var re = new RegExp("^\\d{8}$");
        if (re.test(strDate)) {
            var yyyy = parseInt(strDate.substr(0, 4), 10);
            var mm = parseInt(strDate.substr(4, 2), 10);
            var dd = parseInt(strDate.substr(6, 2), 10);
            var xdata = new Date(yyyy, mm - 1, dd);
            if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd))
                check = true;
            else
                check = false;
        } else
            check = false;
    }
    else {
        var re = new RegExp("^\\d{6}$");
        if (re.test(strDate)) {
            var yyyy = parseInt(strDate.substr(0, 4), 10);
            var mm = parseInt(strDate.substr(4, 2), 10);
            var dd = 1;
            var xdata = new Date(yyyy, mm - 1, dd);
            if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd))
                check = true;
            else
                check = false;
        } else
            check = false;
    }
    return check;
}
/*格式化时间
formatStr:
   yyyy:年
   MM:月
   dd:日
   hh:小时
   mm:分钟
   ss:秒

Date.prototype.toString = function(formatStr) 
{
    var date = this;
    var timeValues = function(){};
    timeValues.prototype = {
       year:function(){
        if(formatStr.indexOf("yyyy")>=0)
        {
            return date.getYear();
        }
        else
        {
            return date.getYear().toString().substr(2);
        }
       },
       elseTime:function(val,formatVal){
        return formatVal>=0?(val<10?"0"+val:val):(val);
       },
       month:function(){
        return this.elseTime(date.getMonth ()+1,formatStr.indexOf("MM"));
       },
       day:function(){
        return this.elseTime(date.getDate(),formatStr.indexOf ("dd"));
       },
       hour:function(){
        return this.elseTime(date.getHours(),formatStr.indexOf ("hh"));
       },
       minute:function(){
        return this.elseTime(date.getMinutes (),formatStr.indexOf("mm"));
       },
       second:function(){
        return this.elseTime(date.getSeconds(),formatStr.indexOf ("ss"));
       }
    };
    var tV = new timeValues();
    var replaceStr = {
       year:["yyyy","yy"],
       month:["MM","M"],
       day:["dd","d"],
       hour:["hh","h"],
       minute:["mm","m"],
       second:["ss","s"]
    };
    for(var key in replaceStr)
    {
       formatStr = formatStr.replace(replaceStr[key][0],eval ("tV."+key+"()"));
       formatStr = formatStr.replace(replaceStr[key][1],eval ("tV."+key+"()"));
    }
    return formatStr;
};
*/
//var date = new Date();
//alert(date.toString("yyyy-MM-dd hh:mm:ss"));
function getnumber(date1,date2)
{
    //默认格式为"20030303",根据自己需要改格式和方法
    var year1 =  date1.substr(0,4);
    var year2 =  date2.substr(0,4); 
    var month1 = date1.substr(4,2);
    var month2 = date2.substr(4,2);

    var day1 = date1.substr(6,2);
    var day2 = date2.substr(6,2);

    temp1 = year1+"/"+month1+"/"+day1;
    temp2 = year2+"/"+month2+"/"+day2;

    var dateaa= new Date(temp1); 
    var datebb = new Date(temp2); 
    var date = datebb.getTime() - dateaa.getTime(); 
    var time = Math.floor(date / (1000 * 60 * 60 * 24));
    //alert(time);
    return time;
}